package com.chis.jmdatatimer.comm;

import com.chis.jm.bean.ChilInquireBean;
import com.chis.jm.bean.ChildSynInfoBean;
import com.chis.jm.bean.jxc.CntstmInfoBean;
import com.chis.jm.bean.qczs.data.CntStmCode;
import com.chis.jm.bean.qczs.data.Trace;
import com.chis.jm.enumbean.CityRedisType;
import com.chis.jm.pojo.Child;
import com.chis.jm.pojo.Inoculation;
import com.chis.jm.pojo.TdBirthinfo;
import com.chis.jm.pojo.TdChilInform;
import com.chis.jm.pojo.TdChileditdate;
import com.chis.jm.pojo.TdChilhabiChng;
import com.chis.jm.pojo.TdChiltransHand;
import com.chis.jm.pojo.TdChiltransInfo;
import com.chis.jm.pojo.TdChiltransUnit;
import com.chis.jm.pojo.TdDisease;
import com.chis.jm.pojo.TdFamilyhis;
import com.chis.jm.pojo.TdIstabu;
import com.chis.jm.pojo.TdRefuseinoc;
import com.chis.jm.pojo.TdSpremind;
import com.chis.jm.pojo.sys.TbSysEmp;
import com.chis.jm.pojo.sys.TsZone;
import com.chis.jm.pojo.zabbix.TjJxcMntExc;
import com.chis.jm.pojo.zabbix.TjMntExc;
import com.chis.jmdatatimer.timerDto.dto.TimerDTO;
import com.chis.jmdatatimer.timerGpDto.dto.TimerGpDTO;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author jw
 * @Description 数据查询更新基类
 * @Company zwx
 * @date 2016年11月28日
 */
public class TxMgmtModule {
    @Resource
    protected TimerDTO dto;
    @Resource
    protected TimerGpDTO gpDTO;

    public void updateJxcExc(Integer rid,byte[] data) throws Exception{
        dto.updateJxcExc(rid,data);
    }

    public void cntstmInfoSearch() throws Exception {
        List<CntstmInfoBean> cntstmInfoBeans = dto.findAllCntstmInfo();

        if (cntstmInfoBeans != null && cntstmInfoBeans.size() > 0) {
            Map<String, Map<String,CntstmInfoBean>> unitmap = new HashMap<>();
            for (CntstmInfoBean cntstmInfoBean : cntstmInfoBeans) {
                cntstmInfoBean.setRetailPrice(cntstmInfoBean.getPrice());
                Map<String,CntstmInfoBean> cntmap = new HashMap<>();
                if(unitmap.containsKey(cntstmInfoBean.getUnitCode().toString())){
                    cntmap = unitmap.get(cntstmInfoBean.getUnitCode().toString());
                }
                cntmap.put(cntstmInfoBean.getGuid(),cntstmInfoBean);
                unitmap.put(cntstmInfoBean.getUnitCode().toString(), cntmap);
            }

            ShardedJedisPool jedisPool = JedisConnect.getInstance().jedisPool;
            ShardedJedis jedis = jedisPool.getResource();
            jedis.select(1);
            for(String unitCode : unitmap.keySet()) {
                Map<String, CntstmInfoBean> cntmap = unitmap.get(unitCode);
                byte[] bytes = jedis.get(unitCode.getBytes());
                List<CntstmInfoBean> resultList = (List<CntstmInfoBean>) JedisSerializeUtil.unserialize(bytes);
                for (CntstmInfoBean t : resultList) {
                    CntstmInfoBean t1 = cntmap.get(t.getGuid());
                    if (t1 != null && !t1.getStmCnt().equals(t.getStmCnt())) {
                        System.out.println("单位编码：" + unitCode + " 库存：" + t.getGuid() + "mysql：" + t1.getStmCnt() + " redis:" + t.getStmCnt());
                    }
                }
            }
        }
    }

    public void cntstmInfoSync() throws Exception {

        //处理内存数据
        List<CntstmInfoBean> cntstmInfoBeans = dto.findAllCntstmInfo();
        Map<String, List<CntstmInfoBean>> map = new HashMap<>();
        if (cntstmInfoBeans != null && cntstmInfoBeans.size() > 0) {
            for (CntstmInfoBean cntstmInfoBean : cntstmInfoBeans) {
                cntstmInfoBean.setRetailPrice(cntstmInfoBean.getPrice());
                List<CntstmInfoBean> cntstmInfoBeans1 = new ArrayList<>();
                if(map.containsKey(cntstmInfoBean.getUnitCode().toString())){
                    cntstmInfoBeans1 = map.get(cntstmInfoBean.getUnitCode().toString());
                }
                cntstmInfoBeans1.add(cntstmInfoBean);
                map.put(cntstmInfoBean.getUnitCode().toString(), cntstmInfoBeans1);
            }
            //保存内存库
            if (map.size() > 0) {
                try {
                    ShardedJedisPool jedisPool = JedisConnect.getInstance().jedisPool;
                    ShardedJedis jedis = jedisPool.getResource();
                    jedis.select(1);
                    //保存内存库
                    for (String s : map.keySet()) {
                        byte[] serialize = JedisSerializeUtil.serialize(map.get(s));
                        jedis.del(s.getBytes());
                        jedis.set(s.getBytes(), serialize);
                    }
                } catch (Exception e) {
                    throw new Exception(e.getMessage());
                }
            }
        }
    }

    public Date findProdate(String ma,String bat){ return dto.findProdate(ma,bat);}
    public void updatestm(String ma,String bat,Date prodate){dto.updatestm(ma,bat,prodate);}

    public void savezone(List<TsZone> list){dto.savezone(list);}



    public List<TjJxcMntExc> findExc() throws Exception {
        return dto.findMntExc1();
    }

    public List<CntStmCode> testHbase() throws Exception{return gpDTO.findCntstmCodeByElecCode();}
    public String testHbase1() throws Exception{return gpDTO.testHbase1();}


    public Integer findmysqliso(String unitcode,String heapcode)throws Exception{return dto.findmysqliso(unitcode,heapcode);}

    public List<TjMntExc> findExcData() throws Exception{return dto.findMntExc();}

    public void deltracebact(Long s1,String s2,String s3) throws Exception{gpDTO.deltracebact(s1,s2,s3);}

    public List<Trace> canbmtb1(String mainCode) throws Exception{return gpDTO.canbmtb1(mainCode);}
    public List<Trace> canbmtb2(String mainCode) throws Exception{return gpDTO.canbmtb2(mainCode);}

    public Integer stmstate(String mainCode) throws Exception{return dto.stmstate(mainCode);}
    public Integer purstate(String mainCode) throws Exception{return dto.purstate(mainCode);}
    public Integer zkstate(String mainCode) throws Exception{return dto.zkstate(mainCode);}



    public List<CntstmInfoBean> findkc(){
        return dto.findJxcCntstmsByStroage();
    }



    public void rmstmcode(List<String> list){
        StringBuilder sb = new StringBuilder();
        for(String t : list){
            sb.append(",'").append(t).append("'");
        }
        List<String> elecCode = gpDTO.findElecCode(sb.substring(1));
        if(elecCode != null && elecCode.size() > 0){
            for(String s : elecCode){
                System.out.println(s);
            }
        }

    }

    public void rmstmcodeRT(List<String> list){
        StringBuilder sb = new StringBuilder();
        for(String t : list){
            sb.append(",'").append(t).append("'");
        }
        List<String> elecCode = gpDTO.findElecCode(sb.substring(1));
        if(elecCode != null && elecCode.size() > 0){
            sb = new StringBuilder();
            for(String s : elecCode){
                sb.append(",'").append(s).append("'");
            }
            System.out.println(sb.substring(1));
            gpDTO.deleteElecCode(sb.substring(1));
        }

    }



    public void testHa(String[] childcodess) {
        try {
            List<Child> list = new ArrayList<>();
            List<TdChiltransInfo> chiltransInfoList = new ArrayList<>();
            List<TdChiltransUnit> beanList = new ArrayList<>();
            List<TdChilhabiChng> beanList3 = new ArrayList<>();
            List<Inoculation> beanList4 = new ArrayList<>();
            List<TdIstabu> beanList5 = new ArrayList<>();
            List<TdDisease> beanList6 = new ArrayList<>();
            List<TdSpremind> beanList7 = new ArrayList<>();
            List<TdRefuseinoc> beanList8 = new ArrayList<>();
            List<TdChileditdate> beanList9 = new ArrayList<>();
            List<TdChiltransHand> beanList10 = new ArrayList<>();
            List<TdFamilyhis> beanList11 = new ArrayList<>();
            List<TdBirthinfo> beanList12 = new ArrayList<>();
            List<ChilInquireBean> beanList13 = new ArrayList<>();
            List<TdChilInform> beanList14 = new ArrayList<>();
            ChildSynInfoBean childSynInfoBean = new ChildSynInfoBean();

            System.out.println("1查询内存库开始" + new Date().getTime());
            for (String childcode : childcodess) {
                //0儿童基本信息库
                RedisFindChildTx tx0 = new RedisFindChildTx(childcode);
                executeSingleJedisQuery(tx0, CityRedisType.ARCHIVES);
                Child child = tx0.getChild();
                if (child != null) {
                    list.add(child);
                }


                //1儿童历史迁移记录表
                RedisFindtransInfoTx tx1 = new RedisFindtransInfoTx(childcode);
                executeSingleJedisQuery(tx1, CityRedisType.ARCHIVES);
                if (tx1.getOldTransInfo() != null) {
                    chiltransInfoList.addAll(tx1.getOldTransInfo());
                }


                //2儿童单位迁移管理表
                RedisFindtransUnitTx tx2 = new RedisFindtransUnitTx(childcode);
                executeSingleJedisQuery(tx2, CityRedisType.ARCHIVES);
                if (tx2.getOldTransUnit() != null) {
                    beanList.addAll(tx2.getOldTransUnit());
                }


                //3儿童户籍县变更表
                RedisFindhabiChngTx tx3 = new RedisFindhabiChngTx(childcode);
                executeSingleJedisQuery(tx3, CityRedisType.ARCHIVES);
                if (tx3.getOldhabiList() != null) {
                    beanList3.addAll(tx3.getOldhabiList());
                }


                //4儿童免疫实种表
                RedisFindInocTx tx4 = new RedisFindInocTx(childcode);
                executeSingleJedisQuery(tx4, CityRedisType.ARCHIVES);
                if (tx4.getInoculationList() != null) {
                    beanList4.addAll(tx4.getInoculationList());
                }


                //5儿童禁忌症
                RedisFindIstabuTx tx5 = new RedisFindIstabuTx(childcode);
                executeSingleJedisQuery(tx5, CityRedisType.ARCHIVES);
                if (tx5.getOldList() != null) {
                    beanList5.addAll(tx5.getOldList());
                }


                //6儿童疾病史
                RedisFindDiseaseTx tx6 = new RedisFindDiseaseTx(childcode);
                executeSingleJedisQuery(tx6, CityRedisType.ARCHIVES);
                if (tx6.getOldList() != null) {
                    beanList6.addAll(tx6.getOldList());
                }


                //7儿童特殊提醒内容
                RedisFindSpremindTx tx7 = new RedisFindSpremindTx(childcode);
                executeSingleJedisQuery(tx7, CityRedisType.ARCHIVES);
                if (tx7.getOldList() != null) {
                    beanList7.addAll(tx7.getOldList());
                }


                //8不接种疫苗表
                RedisFindRefuseInocTx tx8 = new RedisFindRefuseInocTx(childcode);
                executeSingleJedisQuery(tx8, CityRedisType.ARCHIVES);
                if (tx8.getOldList() != null) {
                    beanList8.addAll(tx8.getOldList());
                }


                //9儿童全档案更新时间表
                RedisFindChildEditdateTx tx9 = new RedisFindChildEditdateTx(childcode);
                executeSingleJedisQuery(tx9, CityRedisType.ARCHIVES);
                if (tx9.getChileditdate() != null) {
                    beanList9.add(tx9.getChileditdate());
                }


                //10人工迁出变更表
                RedisFindTransHandTx tx10 = new RedisFindTransHandTx(childcode);
                executeSingleJedisQuery(tx10, CityRedisType.ARCHIVES);
                if (tx10.getList() != null) {
                    beanList10.addAll(tx10.getList());
                }


                //11儿童家族史表
                RedisFindFamilyhisTx tx11 = new RedisFindFamilyhisTx(childcode);
                executeSingleJedisQuery(tx11, CityRedisType.ARCHIVES);
                TdFamilyhis bean11 = tx11.getList();
                if (bean11 != null) {
                    beanList11.add(bean11);
                }


                // 12新生儿童出生状况表
                RedisFindBirthinfoTx tx12 = new RedisFindBirthinfoTx(childcode);
                executeSingleJedisQuery(tx12, CityRedisType.ARCHIVES);
                TdBirthinfo bean12 = tx12.getList();
                if (bean12 != null) {
                    beanList12.add(bean12);
                }


                // 13儿童询问诊表
                RedisFindInquireTx tx13 = new RedisFindInquireTx(childcode);
                executeSingleJedisQuery(tx13, CityRedisType.ARCHIVES);
                if (tx13.getList() != null) {
                    beanList13.addAll(tx13.getList());
                }


                // 14儿童知情告知表
                RedisFindInformTx tx14 = new RedisFindInformTx(childcode);
                executeSingleJedisQuery(tx14, CityRedisType.ARCHIVES);
                if (tx14.getList() != null) {
                    beanList14.addAll(tx14.getList());
                }
            }
            System.out.println("2查询内存库结束" + new Date().getTime());


            childSynInfoBean.setChildList(list);
            childSynInfoBean.setTransInfoList(chiltransInfoList);
            childSynInfoBean.setTransUnitList(beanList);
            childSynInfoBean.setHabiChngList(beanList3);
            childSynInfoBean.setInocList(beanList4);
            childSynInfoBean.setIstabuList(beanList5);
            childSynInfoBean.setDiseaseList(beanList6);
            childSynInfoBean.setSpremindList(beanList7);
            childSynInfoBean.setRefuseinocList(beanList8);
            childSynInfoBean.setUptimeList(beanList9);
            childSynInfoBean.setTransHandList(beanList10);
            childSynInfoBean.setFamilyhisList(beanList11);
            childSynInfoBean.setBirthinfoList(beanList12);
            childSynInfoBean.setInquireBeanList(beanList13);
            childSynInfoBean.setInformList(beanList14);

            if (childSynInfoBean.getChildList() != null && !childSynInfoBean.getChildList().isEmpty()) {
                dto.insertChildList(childSynInfoBean.getChildList());
            }
            if (childSynInfoBean.getInocList() != null && !childSynInfoBean.getInocList().isEmpty()) {
                dto.insertInocList(childSynInfoBean.getInocList());
            }
            if (childSynInfoBean.getFamilyhisList() != null && !childSynInfoBean.getFamilyhisList().isEmpty()) {
                dto.insertFamilyhisList(childSynInfoBean.getFamilyhisList());
            }
            if (childSynInfoBean.getBirthinfoList() != null && !childSynInfoBean.getBirthinfoList().isEmpty()) {
                dto.insertBirthinfoList(childSynInfoBean.getBirthinfoList());
            }
//			if (childSynInfoBean.getInquireBeanList() != null && !childSynInfoBean.getInquireBeanList().isEmpty()) {
//				dto.insertInquireList(childSynInfoBean.getInquireBeanList());
//				//保存询问诊答案
//				List<TdChilAnswer> answerList = new ArrayList<TdChilAnswer>();
//				for (ChilInquireBean t : childSynInfoBean.getInquireBeanList()) {
//					if (t.getAnswerList() != null && t.getAnswerList().size() > 0) {
//						answerList.addAll(t.getAnswerList());
//					}
//				}
//				//先清空旧答案内容，插入新的
//				dto.delAnswerListByMain(childSynInfoBean.getInquireBeanList());
//				if (!answerList.isEmpty()) {
//					dto.insertAnswerList(answerList);
//				}
//			}
            if (childSynInfoBean.getInformList() != null && !childSynInfoBean.getInformList().isEmpty()) {
                dto.insertInformList(childSynInfoBean.getInformList());
            }
            //迁移4表
            if (childSynInfoBean.getTransInfoList() != null && !childSynInfoBean.getTransInfoList().isEmpty()) {
                dto.insertTransList(childSynInfoBean.getTransInfoList());
            }
            if (childSynInfoBean.getTransUnitList() != null && !childSynInfoBean.getTransUnitList().isEmpty()) {
                dto.insertUnitList(childSynInfoBean.getTransUnitList());
            }
            if (childSynInfoBean.getHabiChngList() != null && !childSynInfoBean.getHabiChngList().isEmpty()) {
                dto.insertHabiList(childSynInfoBean.getHabiChngList());
            }
            if (childSynInfoBean.getTransHandList() != null && !childSynInfoBean.getTransHandList().isEmpty()) {
                dto.insertHandList(childSynInfoBean.getTransHandList());
            }
            //异常4表
            if (childSynInfoBean.getIstabuList() != null && !childSynInfoBean.getIstabuList().isEmpty()) {
                dto.insertIstabuList(childSynInfoBean.getIstabuList());
            }
            if (childSynInfoBean.getDiseaseList() != null && !childSynInfoBean.getDiseaseList().isEmpty()) {
                dto.insertDiseaseList(childSynInfoBean.getDiseaseList());
            }
            if (childSynInfoBean.getSpremindList() != null && !childSynInfoBean.getSpremindList().isEmpty()) {
                dto.insertSpecmindList(childSynInfoBean.getSpremindList());
            }
            if (childSynInfoBean.getRefuseinocList() != null && !childSynInfoBean.getRefuseinocList().isEmpty()) {
                dto.insertRefuseList(childSynInfoBean.getRefuseinocList());
            }
            //儿童全档案更新时间
            if (childSynInfoBean.getUptimeList() != null && !childSynInfoBean.getUptimeList().isEmpty()) {
                dto.updateEditDateList(childSynInfoBean.getUptimeList());
            }
            //数字化门诊
            if (childSynInfoBean.getInocStationList() != null && !childSynInfoBean.getInocStationList().isEmpty()) {
                dto.saveOrUpInocstation(childSynInfoBean.getInocStationList());
            }
            //工作量
            if (childSynInfoBean.getInocTempList() != null && !childSynInfoBean.getInocTempList().isEmpty()) {
                dto.saveOrUpInoctemp(childSynInfoBean.getInocTempList());
            }
            System.out.println("3更新mysql成功" + new Date().getTime());
        } catch (Exception e) {
            System.out.println("出现异常");
            e.printStackTrace();

        }


    }

    public void executeSingleJedisQuery(TransactionJedis trans, CityRedisType type) {
        ShardedJedis jedis = null;
        try {
            jedis = JedisConnect.getInstance().jedisPool.getResource();
            trans.run(jedis);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }


    public void testGp() {
        try {
            ChildSynInfoBean bean = null;
            long time1 = System.currentTimeMillis();
            //dealarcSynGp(bean, 1);
            long time2 = System.currentTimeMillis();
            System.out.println("耗时：" +(time2-time1));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }




}